package cn.com.sise.controller;

import cn.com.sise.dao.MajorDao;
import cn.com.sise.pojo.Major;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class MajorController
{
@Resource
private MajorDao majorDao;


//根据部门id查专业
//根据majorId查询专业信息
@RequestMapping(value = "/findMajorByDeptId")//请求地址
@ResponseBody//返回格式
public List<Major> findMajorByDeptId(int deptId)
{
System.out.println("系部id:"+deptId);
List<Major> majors=majorDao.findMajorByDeptId(deptId);
for(Major m:majors){System.out.println(m.getMajorId()+" "+m.getMajorName());}
return majors;
};//function findMajorByMajorId



//跳转到添加专业页面
@RequestMapping(value = "/addMajor",method = RequestMethod.GET)
public String toAddMajor()
{
return "/admin/majorForm.jsp";
};//function toAddMajor

//添加专业信息
@RequestMapping(value = "/addMajor",method = RequestMethod.POST)
public String addMajor(Major major)
{
int rows=majorDao.addMajor(major);
//跳转到另一个方法（直接返回页面则不用加redirect）
return "redirect:findAllMajors";
};//function addMajor

//查询所有专业信息
@RequestMapping("/findAllMajors")
public String findAllMajors(Model model)
{
List<Major> majorList=majorDao.findAllMajors();
model.addAttribute("majorList",majorList);
return "/admin/majorInfo.jsp";
};//function findAllMajors

//根据majorId查询专业信息
@RequestMapping(value = "/findMajorByMajorId")
public String findMajorByMajorId(String majorId,Model model)
{
Major major=majorDao.findMajorByMajorId(majorId);
model.addAttribute("major",major);
return "/admin/updateMajor.jsp";

};//function findMajorByMajorId


//修改专业信息
@RequestMapping(value = "/updateMajor")
public String updateMajor(Major major)
{
int rows=majorDao.updateMajor(major);
return "redirect:findAllMajors";

};//function updateMajor

//根据majorId删除专业信息
@RequestMapping(value = "/deleteMajorByMajorId")
public String deleteMajorByMajorId(String majorId)
{
int rows=majorDao.deleteMajorByMajorId(majorId);
return "redirect:findAllMajors";
};//function deleteMajorByMajorId

//批量删除
@RequestMapping("/deleteMajors")
public String deleteMajorsByIds(String[] id)
{
System.out.println("获取到的id：");
for(String i:id){System.out.print(i+" ");}
System.out.println();

int r=majorDao.deleteMajorsByIds(id);

System.out.println("影响行数："+r);
return "redirect:findAllMajors";
}

};//class MajorController
